package cn.gailvlun.gll.presentation.emotion.util;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.net.emotion.Emotion;
import cn.gailvlun.gll.presentation.emotion.core.BaseAdapter;
import cn.gailvlun.gll.presentation.emotion.core.BaseViewModel;



@Deprecated
public class LikeAnimation {

    public static class Builder {
        private CheckedTextView ctvFunction;
        private LinearLayout llFunction;
        private CheckedTextView ctvLike;
        private TextView tvOperateWindow;
        private Context context;
        private BaseViewModel viewModel;
        private BaseAdapter mAdapter;
        private View parentView;
        private LinearLayout oldLayout;

        public CheckedTextView getCtvFunction() {
            return ctvFunction;
        }

        public Builder setCtvFunction(CheckedTextView ctvFunction) {
            this.ctvFunction = ctvFunction;
            return this;
        }

        public LinearLayout getLlFunction() {
            return llFunction;
        }

        public Builder setLlFunction(LinearLayout llFunction) {
            this.llFunction = llFunction;
            return this;
        }

        public CheckedTextView getCtvLike() {
            return ctvLike;
        }

        public Builder setCtvLike(CheckedTextView ctvLike) {
            this.ctvLike = ctvLike;
            return this;
        }

        public TextView getTvOperateWindow() {
            return tvOperateWindow;
        }

        public Builder setTvOperateWindow(TextView tvOperateWindow) {
            this.tvOperateWindow = tvOperateWindow;
            return this;
        }

        public Context getContext() {
            return context;
        }

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public BaseViewModel getViewModel() {
            return viewModel;
        }

        public Builder setViewModel(BaseViewModel viewModel) {
            this.viewModel = viewModel;
            return this;
        }

        public BaseAdapter getAdapter() {
            return mAdapter;
        }

        public Builder setAdapter(BaseAdapter mAdapter) {
            this.mAdapter = mAdapter;
            return this;
        }

        public View getParentView() {
            return parentView;
        }

        public Builder setParentView(View parentView) {
            this.parentView = parentView;
            return this;
        }

        public LikeAnimation build() {
            return new LikeAnimation(ctvFunction, llFunction, ctvLike, tvOperateWindow, context, viewModel, mAdapter, parentView);
        }

    }

    private CheckedTextView ctvFunction;
    private LinearLayout llFunction;
    private CheckedTextView ctvLike;
    private TextView tvOperateWindow;
    private Context context;
    private BaseViewModel viewModel;
    private BaseAdapter mAdapter;
    private View parentView;
    private static LinearLayout oldLayout;

    public LikeAnimation(CheckedTextView ctvFunction, LinearLayout llFunction, CheckedTextView ctvLike, TextView tvOperateWindow, Context context, BaseViewModel viewModel, BaseAdapter mAdapter, View parentView) {
        this.ctvFunction = ctvFunction;
        this.llFunction = llFunction;
        this.ctvLike = ctvLike;
        this.tvOperateWindow = tvOperateWindow;
        this.context = context;
        this.viewModel = viewModel;
        this.mAdapter = mAdapter;
        this.parentView = parentView;
    }

    public void start(int position) {

        final Emotion emotion = (Emotion) mAdapter.getData().get(position);
        ctvLike.setChecked(false);
        llFunction.setVisibility(View.INVISIBLE);
        llFunction.setTag(FunctionState.CLOSED);
        final Animation compressAnimation = AnimationUtils.loadAnimation(context, R.anim.anim_scale_compress);
        final Animation stretchAnimation = AnimationUtils.loadAnimation(context, R.anim.anim_scale_stretch);
        ctvFunction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                FunctionState tag = (FunctionState) llFunction.getTag();
                FunctionState oldTag = null;
                if (oldLayout != null) {
                    oldTag = (FunctionState) oldLayout.getTag();
                }
                if (oldLayout != null && oldTag != null && oldTag != tag) {
                    oldLayout.setTag(FunctionState.CLOSED);
                    oldLayout.setVisibility(View.INVISIBLE);
                    oldLayout = llFunction;
                } else if (oldTag == tag) {
                    oldLayout = null;
                } else if (oldTag == null && oldLayout == null) {
                    oldLayout = llFunction;

                }

                if (tag != FunctionState.OPENED) {
                    stretchAnimation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                            llFunction.setVisibility(View.VISIBLE);


                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            llFunction.setTag(FunctionState.OPENED);

                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });

                    llFunction.startAnimation(stretchAnimation);
                } else {
                    compressAnimation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                            llFunction.setTag(FunctionState.CLOSED);

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            llFunction.setVisibility(View.INVISIBLE);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                    llFunction.startAnimation(compressAnimation);
                }

            }
        });
        ctvLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_scale_reverse);
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        ctvLike.setChecked(true);
                        viewModel.baseEmotionHeart(ctvLike, emotion);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        llFunction.startAnimation(compressAnimation);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                view.startAnimation(animation);
            }
        });
        tvOperateWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                llFunction.setVisibility(View.INVISIBLE);
                llFunction.setTag(FunctionState.CLOSED);
                viewModel.baseShowOperateEmotionWindow(parentView, emotion);
            }
        });
    }
}
